`
https://leetcode.cn/problems/minimum-time-to-complete-all-deliveries/
`

/**
 * @param {number[]} d
 * @param {number[]} r
 * @return {number}
 */
var minimumTime = function (d, r) {
  // https://leetcode.cn/problems/minimum-time-to-complete-all-deliveries/solutions/3821373/liang-chong-fang-fa-er-fen-da-an-shu-xue-vyqv/
  const [d1, d2] = d, [r1, r2] = r
  const l = lcm(r1, r2)

  function check(t) {
    return d1 <= t - Math.floor(t / r1) && d2 <= t - Math.floor(t / r2) && d1 + d2 <= t - Math.floor(t / l)
  }

  let left = d1 + d2 - 2, right = (d1 + d2) * 2 - 1
  while (left + 1 < right) {
    const mid = Math.floor((right - left) / 2) + left
    if (check(mid)) {
      right = mid
    } else {
      left = mid
    }
  }
  return right
};

function gcd(a, b) {
  a = Math.abs(a);
  b = Math.abs(b);
  while (b !== 0) {
    [a, b] = [b, a % b];
  }
  return a;
}

function lcm(a, b) {
  return Math.abs(a * b) / gcd(a, b);
}